package org.helldorado.companybuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;

// class TitleIdProprtiesDescription
public class TIPD {
	private Integer m_id = 0;
	private int m_totalUses = -1;
	private int m_uses = -1;
	private String m_title = "";
	private String m_description = "";
	private List<String> m_properties;

	public static Map<Integer, TIPD>  getTipdFromFile(String source, String tagName) {	
			
		Map<Integer, TIPD> tipdMap = new HashMap<Integer, TIPD>();
		try {
			Integer resID = MainActivity.getContext().getResources().getIdentifier(source, "xml", "org.helldorado.companybuilder");
			XmlPullParser xpp0= MainActivity.getContext().getResources().getXml(resID);
			
			int eventType0 = xpp0.getEventType();
			
			
			TIPD tipd = null;
			List<String> props = null;
			
			// Loop through pull events until we 4reach END_DOCUMENT
			while (eventType0 != XmlPullParser.END_DOCUMENT) {
				String tagname0 = xpp0.getName();

				// React to different event types appropriately
				switch (eventType0) {
				case XmlPullParser.START_TAG:
					if (tagname0.equalsIgnoreCase(tagName)) {
						tipd = new TIPD();
						props = new ArrayList<String>();
						
						tipd.setTitle(xpp0.getAttributeValue(null, "title"));
						tipd.setId(Integer.parseInt(xpp0.getAttributeValue(null, "id")));
					}
					if (tagname0.equalsIgnoreCase("description")) {
						if (tipd != null) {
							if(xpp0.next() == XmlPullParser.TEXT) {
								tipd.setDescription(xpp0.getText());
							}
						}
					}
					if (tagname0.equalsIgnoreCase("property")) {
						if (props != null) {
							if(xpp0.next() == XmlPullParser.TEXT) {
								props.add(xpp0.getText());
							}
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if (tagname0.equalsIgnoreCase(tagName)) {
						tipd.setProperties(props);
						tipdMap.put(tipd.getId(), tipd);
					}

				default:
					break;
				}
			
				//move on to next iteration
				eventType0 = xpp0.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tipdMap;
	}


	public Integer getId() {
		return m_id;
	}


	protected void setId(Integer m_id) {
		this.m_id = m_id;
	}


	public String getDescription() {
		return m_description;
	}


	protected void setDescription(String m_description) {
		this.m_description = m_description;
	}


	public List<String> getProperties() {
		return m_properties;
	}


	protected void setProperties(List<String> m_properties) {
		this.m_properties = m_properties;
	}


	public String getTitle() {
		return m_title;
	}


	protected void setTitle(String m_title) {
		this.m_title = m_title;
	}


	public int getUses() {
		return m_uses;
	}


	public void setUses(int m_uses) {
		this.m_uses = m_uses;
	}
}
